package com.org.manhu.chargehelper.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.org.manhu.chargehelper.constant.Constant;

import android.text.TextUtils;

/**
 *
 * @文件名：JsonUtil.java
 * @包名：com.org.manhu.tool
 * @作者：李朋辉 @ QQ：892839211
 * @创建时间：2014年11月20日 下午1:32:52
 * @描述：
 */
public class JsonUtil {

	/**
	 * @描述：获取总记录数
	 * @param json
	 * @return
	 */
	public static int getTotalPage(String json) {
		int count = -1;
		if (TextUtils.isEmpty(json)) {
			return count;
		}
		try {
			JSONObject jsonObject = new JSONObject(json);
			count = jsonObject.getInt("AllCount");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count / Constant.PAGE_SIZE + 1;
	}

	/**
	 * @描述：将json字符串解析为list集合,用于列表的解析
	 * @param json
	 * @return
	 */
	public static List<Map<String, String>> parseToList(String json) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		if (TextUtils.isEmpty(json)) {
			return null;
		}

		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONArray array;
			if (jsonObject.has("List")) {
				String retCode = jsonObject.getString("Code");
				String retMsg = jsonObject.getString("Description");
				if ("1".equals(retCode)) {
					array = jsonObject.getJSONArray("List");
				} else {
					return null;
				}
			} else {
				array = jsonObject.getJSONArray("data");
			}
			if (array.length() == 0) {
				// ToastShow.showShortToast(context, retMsg);
			} else {
				// 遍历数组取出数组中的每一个JSONObject
				for (int i = 0; i < array.length(); i++) {
					map = new HashMap<String, String>();
					JSONObject object = array.getJSONObject(i);
					// 获取到JSONObject的key值的游标
					Iterator<String> keys = object.keys();
					// 遍历JSONObject取出JSONObject中的每一个元素
					while (keys.hasNext()) {
						String key = keys.next().toString();
						map.put(key, object.getString(key));
					}
					list.add(map);
				}
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("parseToList   方法中   json解析异常" + e.toString());
		}

		return list;
	}

	/**
	 * @描述：将数据分类解析到不同的集集合中
	 * @param json
	 * @return
	 */
	public static Map<String, List<Map<String, String>>> categoryParse(
			String json) {
		Map<String, List<Map<String, String>>> categoryCollection = new HashMap<String, List<Map<String, String>>>();
		List<Map<String, String>> appList = new ArrayList<Map<String, String>>();
		List<Map<String, String>> gameList = new ArrayList<Map<String, String>>();
		List<Map<String, String>> videoList = new ArrayList<Map<String, String>>();
		List<Map<String, String>> ebookList = new ArrayList<Map<String, String>>();
		categoryCollection.put("应用", appList);
		categoryCollection.put("游戏", gameList);
		categoryCollection.put("视频", videoList);
		categoryCollection.put("电子书", ebookList);
		Map<String, String> map;
		if (TextUtils.isEmpty(json)) {
			return null;
		}

		try {
			JSONObject jsonObject = new JSONObject(json);
			String retCode = jsonObject.getString("Code");
			String retMsg = jsonObject.getString("Description");
			if ("1".equals(retCode)) {
				JSONArray array = jsonObject.getJSONArray("List");
				if (array.length() == 0) {
					// ToastShow.showShortToast(context, retMsg);
				} else {
					// 遍历数组取出数组中的每一个JSONObject
					for (int i = 0; i < array.length(); i++) {
						map = new HashMap<String, String>();
						JSONObject object = array.getJSONObject(i);

						String typeId = object.getString("TypeID");
						// 获取到JSONObject的key值的游标
						Iterator<String> keys = object.keys();
						// 遍历JSONObject取出JSONObject中的每一个元素
						while (keys.hasNext()) {
							String key = keys.next().toString();
							map.put(key, object.getString(key));
						}
						if ("1".equals(typeId)) {
							appList.add(map);
						} else if ("2".equals(typeId)) {
							gameList.add(map);
						} else if ("3".equals(typeId)) {
							videoList.add(map);
						} else if ("5".equals(typeId)) {
							ebookList.add(map);
						}
					}
				}
			} else {
				return null;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("parseToList   方法中   json解析异常" + e.toString());
		}

		return categoryCollection;
	}

	public static Map<String, List<Map<String, String>>> parseBoxData(
			String json) {
		Map<String, List<Map<String, String>>> categoryCollection = new HashMap<String, List<Map<String, String>>>();
		List<Map<String, String>> appList = new ArrayList<Map<String, String>>();
		List<Map<String, String>> gameList = new ArrayList<Map<String, String>>();
		List<Map<String, String>> videoList = new ArrayList<Map<String, String>>();
		List<Map<String, String>> ebookList = new ArrayList<Map<String, String>>();
		categoryCollection.put("应用", appList);
		categoryCollection.put("游戏", gameList);
		categoryCollection.put("视频", videoList);
		categoryCollection.put("电子书", ebookList);
		Map<String, String> map;
		if (TextUtils.isEmpty(json)) {
			return null;
		}

		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONArray array = jsonObject.getJSONArray("data");
			if (array.length() == 0) {
				// ToastShow.showShortToast(context, retMsg);
			} else {
				// 遍历数组取出数组中的每一个JSONObject。
				for (int i = 0; i < array.length(); i++) {
					List<Map<String, String>> list = new ArrayList<Map<String, String>>();
					JSONObject categoryObject = array.getJSONObject(i);

					String typeId = categoryObject.getString("TypeID");

					JSONArray categoryArray = categoryObject
							.getJSONArray("list");
					for (int j = 0; j < categoryArray.length(); j++) {
						map = new HashMap<String, String>();
						JSONObject object = categoryArray.getJSONObject(j);
						// 获取到JSONObject的key值的游标
						Iterator<String> keys = object.keys();
						// 遍历JSONObject取出JSONObject中的每一个元素
						while (keys.hasNext()) {
							String key = keys.next().toString();
							map.put(key, object.getString(key));
						}
						list.add(map);
					}
					if ("1".equals(typeId)) {
						appList.addAll(list);
					} else if ("2".equals(typeId)) {
						gameList.addAll(list);
					} else if ("3".equals(typeId)) {
						videoList.addAll(list);
					} else if ("5".equals(typeId)) {
						ebookList.addAll(list);
					}
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("parseToList   方法中   json解析异常" + e.toString());
		}

		return categoryCollection;
	}

	/**
	 * @描述：将json字符串解析为map集合,用于详情的解析
	 * @param json
	 * @return
	 */
	public static Map<String, String> parseToMap(String json) {
		Map<String, String> map = null;
		if (TextUtils.isEmpty(json)) {
			return map;
		}

		try {
			JSONObject jsonObject = new JSONObject(json);
			String retCode = jsonObject.getString("Code");
			String retMsg = jsonObject.getString("Description");
			if ("1".equals(retCode)) {
				map = new HashMap<String, String>();
				// 获取到JSONObject的key值的游标
				Iterator<String> keys = jsonObject.keys();
				// 遍历JSONObject取出JSONObject中的每一个元素
				while (keys.hasNext()) {
					String key = keys.next().toString();
					map.put(key, jsonObject.getString(key));
				}
			} else {
				return null;
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("parseToMap   方法中   json解析异常" + e.toString());
		}

		return map;
	}

	/**
	 * @描述：将json字符串解析为map集合
	 * @param json
	 * @return
	 */
	public static Map<String, String> parseAll(String json) {
		Map<String, String> map = null;
		if (TextUtils.isEmpty(json)) {
			return map;
		}

		try {
			JSONObject jsonObject = new JSONObject(json);
			map = new HashMap<String, String>();
			// 获取到JSONObject的key值的游标
			Iterator<String> keys = jsonObject.keys();
			// 遍历JSONObject取出JSONObject中的每一个元素
			while (keys.hasNext()) {
				String key = keys.next().toString();
				map.put(key, jsonObject.getString(key));
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("parseToMap   方法中   json解析异常" + e.toString());
		}

		return map;
	}

	/**
	 * @描述：解析图片列表
	 * @param json
	 * @return
	 */
	public static List<String> parseImageList(String json) {
		List<String> list = new ArrayList<String>();
		if (TextUtils.isEmpty(json)) {
			return null;
		}
		try {
			JSONArray array = new JSONArray(json);
			for (int i = 0; i < array.length(); i++) {
				list.add(array.getString(i));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out
					.println("parseImageList   方法中   json解析异常" + e.toString());
		}
		return list;
	}

	public static String getPath(String json) {
		String out = "";
		try {
			JSONObject jsonObject = new JSONObject(json);
			out = jsonObject.getString("path");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return out;
	}

}
